home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8699 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why is Gets() so bad?
  5. Date: 03 Mar 1996 22:30:40 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar3153040@qcd.lanl.gov>
  8. References: <4hb1ie$pa7@ixnews2.ix.netcom.com> <4hcvef$308@qns2.qns.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: mjarvis@qns2.qns.com's message of 3 Mar 1996 14:29:03 -0600
  13.  
  14. In article <4hcvef$308@qns2.qns.com>
  15. mjarvis@qns2.qns.com (Michael Jarvis) writes:
  16.  
  17. <snip>
  18. MJ:         if ( fgets( buf, sizeof(buf)-1, stdin ) == NULL )
  19.  
  20. The fgets function reads at most one less than the number of
  21. characters specified by its second argument. It appends a '\0' (unless
  22. an eof took place before it read anything): so the total number of
  23. characters written cannot exceed the number specified.
  24.  
  25. MJ:                 perror( "Error reading in gets()");
  26. MJ: 
  27. MJ:         printf( "The buf=\"%s\"\n", buf );
  28.  
  29. You should be careful about fgets: it retains the newline character
  30. (provided it has space of course). Note that if fgets fails, you
  31. should not use buf without checking further: the contents could be
  32. indeterminate. 
  33.  
  34. Cheers
  35. Tanmoy
  36. --
  37. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  38. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  39. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  40. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  41. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  42. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  43.